package com.lt.base;

import android.os.Message;

import com.lt.listener.DataServiceListener;

import java.lang.ref.SoftReference;

public class BaseDataService {
    private SoftReference<DataServiceListener> softDataServiceListener;

    public BaseDataService(DataServiceListener listener) {
        softDataServiceListener = new SoftReference<>(listener);
    }

    protected void setDataServiceListener(DataServiceListener listener) {
        softDataServiceListener = new SoftReference<>(listener);
    }

    protected SoftReference<DataServiceListener> getSoftDataServiceListener() {
        return softDataServiceListener;
    }

    protected void sendMessage(Message message) {
        if (softDataServiceListener != null && softDataServiceListener.get() != null) {
            softDataServiceListener.get().sendMessage(message);
        }
    }

    protected void sendEmptyMessage(int what) {
        Message message = new Message();
        message.what = what;
        sendMessage(message);
    }

    public void removeCallBack() {
        if (softDataServiceListener != null) {
            softDataServiceListener.clear();
        }
    }
}
